# coding:utf-8

import os
import re
import jieba
import jieba.posseg as pseg
from collections import Counter

# 当前文件所在目录 → services/wordcloud/
base_dir = os.path.dirname(os.path.abspath(__file__))

# 用户自定义词典: services/wordcloud/doc/userdict.txt
userdict_path = os.path.join(base_dir, 'doc', 'userdict.txt')
if os.path.exists(userdict_path):
    jieba.load_userdict(userdict_path)

STOPWORDS_FILES = [
    'stopSingleWords.txt',
    'baidu_stopwords.txt',
    'hit_stopwords.txt',
    'scu_stopwords.txt',
    'custom_stopwords.txt'
]

def load_stopwords_from_files(files, base_path):
    stopwords = set()
    for file in files:
        fp = os.path.join(base_path, file)
        if os.path.exists(fp):
            with open(fp, 'r', encoding='utf-8') as f:
                for line in f:
                    word = line.strip()
                    if word:
                        stopwords.add(word)
    return stopwords

def get_word_freq(text: str, min_freq: int = 2) -> dict:
    """
    返回词频字典 {词: 频率}，用于 generate_from_frequencies
    """
    # 停用词目录：services/wordcloud/doc/
    doc_dir = os.path.join(base_dir, 'doc')
    stopwords = load_stopwords_from_files(STOPWORDS_FILES, doc_dir)
    
    # 清洗文本
    text = re.sub(r'https?://\S+', '', text)
    text = re.sub(r'@\S+', '', text)
    text = re.sub(r'[\U00010000-\U0010ffff]', '', text)
    text = re.sub(r'%[0-9A-Fa-f]{2,}', ' ', text) 
    text = re.sub(r'magnet:\S+', '', text)
    text = re.sub(r'[^\u4e00-\u9fa5a-zA-Z0-9\s，。！？；：""''（）【】]', ' ', text)

    words = pseg.lcut(text)
    allowed_flags = {'n', 'nr', 'ns', 'nt', 'nz', 'v', 'vn', 'a'}
    SPECIAL_ENGLISH = {'al', 't1', 'lpl', 'faker', 'hope', 'blg', 'tes', 'kt', 'gen'}
    
    filtered = []
    for word, flag in words:
        w = word.strip()
        if not w or len(w) <= 1:
            continue
        if w in stopwords or re.match(r'^[0-9]+$', w):
            continue
        if re.match(r'^[a-zA-Z]+$', w):
            if w.lower() in SPECIAL_ENGLISH:
                filtered.append(w.upper())
            continue
        if any(f in flag for f in allowed_flags):
            filtered.append(w)
    
    counter = Counter(filtered)
    word_freq = {word: freq for word, freq in counter.items() if freq >= min_freq}
    
    if not word_freq:
        raise ValueError("预处理后无有效词汇，请检查停用词表或输入文本")
    
    return word_freq